
package services;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import database.*;
import java.text.DateFormat;
import mapping.Article;

public class ArticleService {

    public ArticleService() {
    }
    
    
    // Ajout article
        public int addArticle(Article article) {
                int result=0;
               
                DbManager connect = new DbManager();
		Connection conn = null;
		conn = connect.Open_ConnectDB();
		DbProcess process = new DbProcess();
		//Query queryString = new Query();
//		String table = "Article";
//		String[] fields = {"description_article","img_article","date_create_article","etat","id_categorie","id_utilisateur"};
//		String[] value = { article.getDescrip_article(), article.getImg_article(), article.getDate_create_article(),article.getEtat_article(),String.valueOf(article.getId_categorie()),String.valueOf(article.getId_utilisateur()) };
		
                //String t = queryString.Query_Insert(table, fields, value);
                String t = "insert into Article (description_article,img_article,date_create_article,etat,id_categorie,id_utilisateur)";
                t += " values ('"+article.getDescrip_article()+"',"
                        + "'"+ article.getImg_article()+"',"
                        + "'"+article.getDate_create_article()+"',"
                        + "'"+article.getEtat_article()+"',"
                        + ""+article.getId_categorie()+","
                        + ""+article.getId_utilisateur()+")";
                System.out.println(t);
		if (process.Execute_DB(conn, t))
                { 
                    result=1;
                }
		connect.Close_ConnectDB(conn);
               
                return result;
	}
        
        public int getLastIdArticle(){
            DbManager connect = new DbManager();
		Connection conn = null;
		conn = connect.Open_ConnectDB();
		DbProcess process = new DbProcess();
            int rRes= 0;
            String queryString = "select id_article from Article order by id_article desc limit 1 ";
            try {
			ResultSet result  = process.Get_DB(conn, queryString);
			if(result!=null){
				if (result.next()) {
                                    rRes = result.getInt(1);
					
				}	
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
            return rRes;
        }
        
        public boolean updateArticle(Article article,int id_article){
            boolean bRes = false;
            DbManager connect = new DbManager();
            Connection conn = null;
            conn = connect.Open_ConnectDB();
            DbProcess process = new DbProcess();
            
            String queryString = "update Article set description_article = '"+article.getDescrip_article()+"',"
                       + " id_categorie = "+article.getId_categorie()+" where id_article = "+id_article;
            
            if (process.Execute_DB(conn, queryString)){
               
                bRes=true;
            }
		connect.Close_ConnectDB(conn);
            return bRes;
        }
        
        public boolean updateArticleWithImage(Article article,int id_article){
            boolean bRes = false;

            DbManager connect = new DbManager();
            Connection conn = null;
            conn = connect.Open_ConnectDB();
            DbProcess process = new DbProcess();
            
            String queryString = "update Article set description_article = '"+article.getDescrip_article()+"',"
                       + " id_categorie= "+article.getId_categorie()+","
                        + "img_article = '"+article.getImg_article()+"' where id_article = "+id_article;
            
            if (process.Execute_DB(conn, queryString)){
                
                bRes=true;
            }
		connect.Close_ConnectDB(conn);
            return bRes;
        }
        
         public List<Article> findAllArticle(int id_utilisateur) {
		List<Article> listAllArticle = new ArrayList<Article>();
		DbManager connect = new DbManager();
		Connection conn = null;
		conn = connect.Open_ConnectDB();
		DbProcess process = new DbProcess();
		Query queryString = new Query();
		String t = "select * from Article where id_utilisateur="+id_utilisateur+" order by id_article desc";
                System.out.println(t);
		ResultSet result = null;
		try {
			result = process.Get_DB(conn, t);
                       if(result!=null){
                           while (result.next()) {
				Article article = new Article();
                                article.setId_article(result.getInt("id_article"));
                                article.setId_utilisateur(result.getInt("id_utilisateur"));
                                
                                
                                Date date_create = result.getDate("date_create_article");
                                Date heure_create = result.getTime("date_create_article");
                                DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
                                String dateName = dateFormat.format(date_create);
                                
                                DateFormat heurFormat = new SimpleDateFormat("HH:mm:ss");
                                String heurName = heurFormat.format(heure_create);
                                dateName += " a "+heurName;
                                article.setDate_create_article(dateName);
                                
                                article.setDescrip_article(result.getString("description_article"));
                                article.setImg_article(result.getString("img_article"));
                                article.setEtat_article(result.getString("etat"));
                                article.setId_categorie(result.getInt("id_categorie"));
				listAllArticle.add(article);
                           }
                       }
		} catch (Exception e) {
			System.out.println("Connexion failed");
		}
		
		connect.Close_ConnectDB(conn);
		return listAllArticle;

	} 
        
          public List<Article> findAllArticle1() {
		List<Article> listAllArticle = new ArrayList<Article>();
		DbManager connect = new DbManager();
		Connection conn = null;
		conn = connect.Open_ConnectDB();
		DbProcess process = new DbProcess();
		Query queryString = new Query();
		String t = "select * from Article order by id_article desc";
                System.out.println(t);
		ResultSet result = null;
		try {
			result = process.Get_DB(conn, t);
                       if(result!=null){
                           while (result.next()) {
				Article article = new Article();
                                article.setId_article(result.getInt("id_article"));
                                article.setId_utilisateur(result.getInt("id_utilisateur"));
                                
                                
                                Date date_create = result.getDate("date_create_article");
                                Date heure_create = result.getTime("date_create_article");
                                DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
                                String dateName = dateFormat.format(date_create);
                                
                                DateFormat heurFormat = new SimpleDateFormat("HH:mm:ss");
                                String heurName = heurFormat.format(heure_create);
                                dateName += " a "+heurName;
                                article.setDate_create_article(dateName);
                                
                                article.setDescrip_article(result.getString("description_article"));
                                article.setImg_article(result.getString("img_article"));
                                article.setEtat_article(result.getString("etat"));
                                article.setId_categorie(result.getInt("id_categorie"));
				listAllArticle.add(article);
                           }
                       }
		} catch (Exception e) {
			System.out.println("Connexion failed");
		}
		
		connect.Close_ConnectDB(conn);
		return listAllArticle;

	} 
         
         public boolean updateDisponibilite(String etat,int id_article){
            boolean bRes = false;
            DbManager connect = new DbManager();
            Connection conn = null;
            conn = connect.Open_ConnectDB();
            DbProcess process = new DbProcess();
            
            String queryString = "update Article set etat = '"+etat+"' where id_article = "+id_article;
            if (process.Execute_DB(conn, queryString)){
               
                bRes=true;
            }
		connect.Close_ConnectDB(conn);
            return bRes;
        }
}
